#include "HashTable.h"

void TestHash1()
{
	int arr[] = { 1, 11, 4, 15, 26, 7, 44 }; // 在加一个数据就会扩容
	CloseHash::HashTable<int, int> ht;
	for (const auto& e : arr)
	{
		ht.Insert(make_pair(e, e));
	}
	ht.Print();

	ht.Erase(4);
	cout << ht.Find(44) << endl; // ht.Find(44)->_kv.first就能取出数据
	cout << ht.Find(100) << endl; // 不过要先判断是否为空，否则会崩
	cout << ht.Find(4) << endl;
	ht.Print();

	ht.Insert(make_pair(-2, -2));
	ht.Print();

	cout << ht.Find(-2) << endl;
}

void TestHash2()
{
	string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜", "苹果", "香蕉", "苹果", "香蕉" };

	//CloseHash::HashTable<string, int> countHT;
	HashBucket::HashTable<string, int> countHT;
	for (const auto& str : arr)
	{
		auto ptr = countHT.Find(str);
		if (ptr)
		{
			ptr->_kv.second++;
		}
		else
		{
			countHT.Insert(make_pair(str, 1));
		}
	}
}

void TestHash3() // 哈希桶测试统计次数的可以用TestHash2改下命名空间
{
	int arr[] = {1, 11, 4, 15, 26, 7, 44, 55, 99, 78};
	HashBucket::HashTable<int, int> ht;
	for (const auto& e : arr)
	{
		ht.Insert(make_pair(e, e));
	}

	ht.Insert(make_pair(22, 22)); // 刚好扩容
}

void TestHash4()
{
	int n = 10000;
	vector<int> v;
	v.reserve(n);
	srand(time(0));
	for (int i = 0; i < n; ++i)
	{
		//v.push_back(i);
		v.push_back(rand() + i);  // 重复少
		//v.push_back(rand());  // 重复多
	}

	size_t begin1 = clock();
	HashBucket::HashTable<int, int> ht;
	for (auto e : v)
	{
		ht.Insert(make_pair(e, e));
	}
	size_t end1 = clock();

	cout << "数据个数:" << ht.Size() << endl;
	cout << "表的长度:" << ht.TablesSize() << endl;
	cout << "桶的个数:" << ht.BucketNum() << endl;
	cout << "平均每个桶的长度:" << (double)ht.Size() / (double)ht.BucketNum() << endl;
	cout << "最长的桶的长度:" << ht.MaxBucketLenth() << endl;
	cout << "负载因子:" << (double)ht.Size() / (double)ht.TablesSize() << endl;
}

int main()
{
	TestHash2();
	TestHash3();
	TestHash4();

	return 0;
}